<template>
  <div class="table-container">
    <div class="search-container">
      <el-input
        v-model="input"
        style="width: 200px"
        class="mr20"
        placeholder="请输入内容"
      ></el-input>
      <el-select v-model="gender" class="mr20" @change="search" clearable placeholder="请选择性别">
        <el-option label="男" value="1"></el-option>
        <el-option label="女" value="2"></el-option>
      </el-select>
      <el-select v-model="categoryId" class="mr20" @change="search" clearable placeholder="请选择角色">
        <el-option label="1" value="1"></el-option>
        <el-option label="2" value="2"></el-option>
      </el-select>
      <el-button type="primary" @click="search">搜索</el-button>
      <el-button type="primary" @click="add()">新增</el-button>
    </div>
    <el-table
      :data="tableData"
      style="width: 100%; margin-top: 30px"
      border
      tooltip-effect="dark"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55"> </el-table-column>
      <el-table-column align="center" label="姓名" width="220">
        <template slot-scope="scope">
          {{ scope.row.name }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="头像" width="220">
        <template slot-scope="scope">
          <img :src="scope.row.coverImage" alt="" style="width: 50px; height: 50px;">
        </template>
      </el-table-column>
      <el-table-column prop="gender" label="性别" width="">
        <template slot-scope="scope">
          {{ scope.row.gender === 1 ? '男' : '女' }}
        </template>
      </el-table-column>
      <el-table-column prop="date" label="创建时间" width="">
        <template slot-scope="scope">
          {{ scope.row.createTime }}
        </template>
      </el-table-column>
      <el-table-column prop="description" label="描述" width="">
        <template slot-scope="scope">
          {{ scope.row.description }}
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="200">
        <template slot-scope="scope">
          <el-button type="text" @click="add(scope.row.id)" size="small">编辑</el-button>
          <el-button
            type="text"
            size="small"
            @click="handleDelete(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination-container">
      <el-pagination
        :page-size="pageSize"
        :pager-count="11"
        layout="total,prev, pager, next, sizes"
        :page-sizes="[10, 20, 30, 40]"
        :total="total"
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { characterList } from '@/api/ai'
export default {
  name: "Table",
  data() {
    return {
      input: "",
      page: 1,
      pageSize: 20,
      total: 0,
      tableData: [
      ],
      gender: '',
      categoryId: '',
      multipleSelection: [],
    };
  },
  mounted() {
    this.getList()
  },
  methods: {
    getList() {
      characterList(
        {
          page: this.page,
          pageSize: this.pageSize,
          name: this.input,
          gender: this.gender,
          categoryId: this.categoryId
        }
      ).then(res => {
        if (res.code === 0) {
          this.tableData = res.data.data
          this.total = res.data.totalPage
        }
      })
    },
    search() {
      this.page = 1
      this.getList()
    },
    handleDelete(id) {
      console.log(id);
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    handleCurrentChange(val) {
      this.page = val;
      this.getList()
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.getList()
    },
    add(id) {
        this.$router.push(id ? `/table/addorUpdate?id=${id}` : `/table/addorUpdate`)
    },
  },
};
</script>
<style lang="scss" scoped>
.table-container {
  padding: 20px;
  .mr20 {
    margin-right: 20px;
  }
  .search-container {
    &>*{
      margin-bottom: 10px;
    }
  }
}
.pagination-container {
  margin-top: 20px;
  text-align: right;
}
</style>
